package com.umbrella.worker.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.umbrella.worker.dao.WAttachmentMapper;
import com.umbrella.worker.dao.WCertificoreMapper;
import com.umbrella.worker.dto.CertificoreDO;
import com.umbrella.worker.entity.WCertificore;
import com.umbrella.worker.entity.WCertificoreExample;
import com.umbrella.worker.result.ResultDO;
import com.umbrella.worker.result.ResultSupport;
import com.umbrella.worker.service.ICertificoreService;
import com.umbrella.worker.util.BeanUtilsExtends;
import com.umbrella.worker.util.StringUtil;

@Service("certificoreService")
public class CertificoreServiceImpl  extends BaseServiceImpl implements ICertificoreService {
	private static Logger logger = Logger.getLogger(CertificoreServiceImpl.class);
	
	@Autowired
	private WCertificoreMapper certificoreMapper;
	@Autowired
	private WAttachmentMapper attachmentMapper;
	
	@Transactional
	@Override
	public ResultDO create(CertificoreDO certificoreDO) {
		WCertificore certificore = new WCertificore();
		
		ResultSupport result = BeanUtilsExtends.copy(certificore, certificoreDO);
		
		if(!result.isSuccess()) {
			return result;
		}
		
		int recordNum = -1;
		
		certificore.setDatalevel(1);
		certificore.setStatus(1);
		certificore.setCreateTime(Calendar.getInstance().getTime());
		certificore.setModifiTime(Calendar.getInstance().getTime());
		try {
			recordNum = certificoreMapper.insertSelective(certificore);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			logger.error("[obj:certificore][opt:create][msg:" + e.getMessage()
			+ "]");
			return result;
		}
		
		if(recordNum == 1) {
			result.setModel(ResultDO.FIRST_MODEL_KEY, certificore.getId());
		} else {
			result.setSuccess(false);
		}
		return result;
	}
	
	
	/**
	 * 添加家政人员的证件信息
	 * @param staffId
	 * @param attac
	 * @param name
	 * @param author
	 * @return
	 */
	@Override
	public Integer createCertificore(Integer staffId,String attac,String name,String author){
		Integer num=0;
		if(staffId!=null && attac!=null && !("".equals(attac)) && author!=null){
			CertificoreDO obj=new CertificoreDO();
			obj.setCreateAuthor(author);
			obj.setModifiAuthor(author);
			obj.setwCeName(name);
			obj.setwCeStaffId(staffId);
			obj.setwCeAttacId(Integer.parseInt(attac));
			obj.setwCeValidTime(new Date());
			
			ResultDO result =create(obj);
			if(result.isSuccess()){
				num=1;
			}
		}
		return num;
	}


	@Override
	public ResultDO list(CertificoreDO certificoreDO) {
		ResultSupport result = new ResultSupport();
		WCertificoreExample example=new WCertificoreExample();
		WCertificoreExample.Criteria c = example.createCriteria();
		if(StringUtil.isGreatOne(certificoreDO.getwCeStaffId())) {
			c.andWCeStaffIdEqualTo(certificoreDO.getwCeStaffId());
		}
		c.andDatalevelEqualTo(1);
		example.setOrderByClause(" CREATE_TIME DESC");
		
		List<WCertificore> list=null;
		
		
		try {
			list = certificoreMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:certificore][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<CertificoreDO> list2 =getCertificoreDOList(list);
		
		List<CertificoreDO> list3 = new ArrayList<CertificoreDO>();
		
		if(list2 != null) {
			for(int i = 0; i < list2.size(); i++) {
				CertificoreDO certificore= list2.get(i);
				if(certificore != null) {
					//WAttachment attc=attachmentMapper.selectByPrimaryKey(certificore.getwCeAttacId());
					
					list3.add(certificore);
				}
			}
		}
		result.setModel(ResultSupport.FIRST_MODEL_KEY, list3);
		return result;
	}

}
